package com.qfedu.servlet;

import com.qfedu.entity.Cart;
import com.qfedu.entity.Order;
import com.qfedu.entity.Result;
import com.qfedu.entity.User;
import com.qfedu.service.IAddressService;
import com.qfedu.service.ICartService;
import com.qfedu.service.IOrderService;
import com.qfedu.service.impl.AddressService;
import com.qfedu.service.impl.CartServiceImpl;
import com.qfedu.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/cart")
public class CartServlet extends BaseServlet {
    private ICartService cartService = new CartServiceImpl();
    private IOrderService orderService = new OrderServiceImpl();
    private IAddressService addressService = new AddressService();
    private List<Order> orderList;
    public void addCart(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String pidStr = request.getParameter("pid");
        System.out.println(pidStr);
        User user = (User) request.getSession().getAttribute("user");
        int uid = user.getId();
        cartService.insertCart(uid,Integer.parseInt(pidStr));
        try {
            request.getRequestDispatcher("cartSuccess.jsp").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        }
    }
    public void findCart(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = (User) request.getSession().getAttribute("user");
        if (user==null){
            response.sendRedirect("login.jsp");
        }else {
            int uid = user.getId();
            Result result = cartService.cartList(uid);
            List<Cart> carts = cartService.getCartList(uid);
            System.out.println(result);
            try {
                if (result.isSuccess()){
                    request.getSession().setAttribute("cartList",carts);
                    request.getRequestDispatcher("cart.jsp").forward(request,response);
                }else {
                    request.getSession().setAttribute("cartList",carts);
                    response.sendRedirect("cart.jsp");
                }
            } catch (ServletException e) {
                e.printStackTrace();
            }
        }


    }
    public void subtractCart(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String pidStr = request.getParameter("pid");
        System.out.println(pidStr);
        User user = (User) request.getSession().getAttribute("user");
        int uid = user.getId();
        cartService.subtractCart(uid,Integer.parseInt(pidStr));
        try {
            request.getRequestDispatcher("cart?method=findCart&uid=" + uid).forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        }
    }
    public void addCartCount(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String pidStr = request.getParameter("pid");
        System.out.println(pidStr);
        User user = (User) request.getSession().getAttribute("user");
        int uid = user.getId();
        cartService.insertCart(uid,Integer.parseInt(pidStr));
        try {
            request.getRequestDispatcher("cart?method=findCart&uid=" + uid).forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        }
    }
    public void changeCartCount(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String pidStr = request.getParameter("pid");
        System.out.println(pidStr);
        User user = (User) request.getSession().getAttribute("user");
        int uid = user.getId();
        String count = request.getParameter("count");
        cartService.changeCartCount(uid,Integer.parseInt(pidStr),Integer.parseInt(count));
    }
    public void deleteCart(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String pidStr = request.getParameter("pid");
        int pid = Integer.parseInt(pidStr);
        System.out.println(pidStr);
        User user = (User) request.getSession().getAttribute("user");
        int uid = user.getId();
        cartService.deleteCart(uid,Integer.parseInt(pidStr));
        try {
            request.getRequestDispatcher("cart?method=findCart&pid=" + pid).forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        }
    }
    public void clearCart(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = (User) request.getSession().getAttribute("user");
        int uid = user.getId();
        cartService.clearCart(uid);
        try {
            request.getRequestDispatcher("cart?method=findCart&uid="+uid).forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        }
    }
    public void jumpCart(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = (User) request.getSession().getAttribute("user");
        int uid = user.getId();
        Result result = addressService.queryAddressList(uid);
        Result result1 = cartService.cartList(uid);
        request.getSession().setAttribute("detailList",result.getDate());
        try {
            if (!result1.isSuccess()){
                request.getRequestDispatcher("index.jsp").forward(request,response);
            }else {
                request.getRequestDispatcher("order.jsp").forward(request,response);
            }
        } catch (ServletException e) {
            e.printStackTrace();
        }
    }
}
